/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.security;

import javax.servlet.http.HttpSessionEvent;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.session.HttpSessionEventPublisher;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * EnhancedHttpSessionEventPublisher.java
 * 
 * @author Administrator
 */
public class EnhancedHttpSessionEventPublisher
		extends
			HttpSessionEventPublisher {
	@Override
	public void sessionCreated(HttpSessionEvent event) {
		// 将用户加入到在线用户列表中
		saveOrDeleteOnlineUser(event, Type.SAVE);
		super.sessionCreated(event);
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		// 将用户从在线用户列表中移除
		saveOrDeleteOnlineUser(event, Type.DELETE);
		super.sessionDestroyed(event);
	}

	public void saveOrDeleteOnlineUser(HttpSessionEvent event, Type type) {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		if (auth != null) {
			Object principal = auth.getPrincipal();
			if (principal instanceof User) {
				User user = (User) principal;
				WebApplicationContext wac = WebApplicationContextUtils
						.getRequiredWebApplicationContext(event.getSession()
								.getServletContext());
				OnlineUserService onlineUserService = (OnlineUserService) wac
						.getBean("onlineUserService");
				switch (type) {
					case SAVE :
						onlineUserService.saveOnlineUser(user.getUsername());
						break;
					case DELETE :
						onlineUserService.deleteOnlineUser(user.getUsername());
						break;
				}
			}
		}
	}

	/**
	 * 定义一个简单的内部枚举
	 */
	private static enum Type {
		SAVE, DELETE;
	}
}
